package com.bto.solar.work.service;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.dao.IDesignService;
import com.bto.solar.work.dto.order.DesignDto;
import com.bto.solar.work.entity.Design;
import com.bto.solar.work.entity.OrderState;
import com.bto.solar.work.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王小波
 * @description:
 * @date 2023/10/31 11:50
 */
@Service
public class BtoDesignService {
    @Autowired
    private IDesignService designService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;

    public String getCurrentDesignTaskId(String orderId) {
        OrderState updateTaskState = btoOrderStateService.getOrderTaskState(orderId, BizStageEnum.DESIGN.getStageId(), BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskId());
        if(NullUtils.isNotNull(updateTaskState)){
            return BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskId();
        }
        else {
            OrderState orderTaskState = btoOrderStateService.getOrderTaskState(orderId, BizStageEnum.DESIGN.getStageId(), BizTaskEnum.TASK_CSPSXX.getTaskId());
            if(NullUtils.isNotNull(orderTaskState)){
                return BizTaskEnum.TASK_CSPSXX.getTaskId();
            }
            else {
                return null;
            }
        }

    }

    /**
     * 获取设计的设备规格信息
     */
    public List<DesignDto.DesignDevice> getOrderDesignDeviceSpecInfo(String orderId, String deviceType) {
        Design design = getLatestDesign(orderId, true);
        if (NullUtils.isNull(design))
            return Collections.emptyList();

        List<DesignDto.DesignDevice> designDevices = design.getDesignDevice();

        if (NullUtils.isNull(designDevices))
            return Collections.emptyList();
        return designDevices.stream().filter(designDevice -> designDevice.getDeviceType().equals(deviceType)).collect(Collectors.toList());
    }


    /**
     * 获取最新的设计信息
     */
    public Design getLatestDesign(String orderId, boolean pass) {
        return designService.lambdaQuery()
                .eq(Design::getOrderId, orderId)
                .eq(pass, Design::getState, BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId())
                .orderByDesc(Design::getCreateTime).last("LIMIT 1").one();
    }

    /**
     * 获取最新的设计信息
     */
    public Design getFirestDesign(String orderId) {
        return designService.lambdaQuery()
                .eq(Design::getOrderId, orderId)
                .orderByAsc(Design::getCreateTime).last("LIMIT 1").one();
    }

    /**
     * 获取最新的设计信息
     */
    public void updateLatestDesignState(String orderId, String designTaskState) {
        Design design = designService.lambdaQuery().eq(Design::getOrderId, orderId)
                .orderByDesc(Design::getCreateTime).last("LIMIT 1").one();
        design.setState(designTaskState);
        designService.updateById(design);
    }


    /**
     * 获取最近设计信息
     */
    public List<Design> getRecentlyDesign(String orderId) {
        return designService.lambdaQuery().eq(Design::getOrderId, orderId)
                .orderByDesc(Design::getCreateTime).list();
    }

    /**
     * 获取最新的设计信息
     */
    public void crateLatestDesignState(String orderId, String designTaskState) {
        Design design = designService.lambdaQuery().eq(Design::getOrderId, orderId)
                .orderByDesc(Design::getCreateTime).last("LIMIT 1").one();
        design.setState(designTaskState);
        designService.updateById(design);
    }
}
